package main

import (
	"gorm.io/gen"
	"mall_admin_api/biz/config"
	"mall_admin_api/biz/dal/db"
	"mall_admin_api/cmd/method"
)

func main() {
	config.MustInit()
	db.MustInit()

	g := gen.NewGenerator(gen.Config{
		FieldSignable: false,
		Mode:          gen.WithDefaultQuery,
		OutPath:       "biz/dal",
		ModelPkgPath:  "biz/model",
	})
	g.UseDB(db.GetDB())

	g.WithDataTypeMap(map[string]func(detailType string) (dataType string){
		"int": func(detailType string) string {
			return "int64"
		},
		"tinyint": func(detailType string) string {
			return "int64"
		},
	})

	g.ApplyInterface(func() {},
		g.GenerateModel("admin_menu",
			gen.FieldType("type", "common.MenuType")),
		g.GenerateModel("admin_role"),
		g.GenerateModel("administrator"),
		g.GenerateModel("admin_role_menu_relation"),
		g.GenerateModel("admin_role_relation"),
		g.GenerateModel("biz"),
		g.GenerateModel("category"),
		g.GenerateModel("product",
			gen.FieldType("status", "common.ProductStatus"),
			gen.FieldType("spec_type", "common.ProductSpecType"),
		),
		g.GenerateModel("product_material_group"),
		g.GenerateModel("product_material",
			gen.FieldType("type", "common.MaterialType"),
		),
		g.GenerateModel("orders",
			gen.FieldType("status", "common.OrderStatus"),
			gen.FieldType("pay_status", "common.PayStatus"),
		),
		g.GenerateModel("orders_item"),
		g.GenerateModel("user"),
		g.GenerateModel("orders_log"),
		g.GenerateModel("express"),
		g.GenerateModel("product_item"),
		g.GenerateModel("product_spec_value"),
		g.GenerateModel("product_spec"),
		g.GenerateModel("product_specifications_template"),
		g.GenerateModel("user_points_log",
			gen.FieldType("type", "common.UserPointsType"),
		),
	)

	noBizIDTableName := []string{
		"AdminRole", "AdminMenu", "AdminRoleMenuRelation",
		"AdminRoleRelation", "Administrator", "Biz", "Region",
	}

	var allTableNames []string
	for tableName, _ := range g.Data {
		if err := method.GenerateExtraMethod(tableName, noBizIDTableName, g.OutPath); err != nil {
			panic(err)
		}
		allTableNames = append(allTableNames, tableName)
	}

	if err := method.GenInitDAO(allTableNames, g.OutPath); err != nil {
		panic(err)
	}
	g.Execute()
}
